package com.hp.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class WeatherUtil {



    /**
     * 获取对应城市的天气
     * @param cityName
     * @return
     */
    public static String GetWeatherInform(String cityName) {
        String baiduUrl = "";
        StringBuffer strBuf;
        try {
            baiduUrl="http://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=dOtCNhkwmyb4T7OD8Mj344HZPC49Cahr" ;
            //baiduUrl ="http://api.map.baidu.com/telematics/v3/weather?location=" + URLEncoder.encode(cityName, "utf-8") + "&output=json&ak=dOtCNhkwmyb4T7OD8Mj344HZPC49Cahr";
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        strBuf = new StringBuffer();
        try {
            URL url = new URL(baiduUrl);
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));//转码。
            String line = null;
            while ((line = reader.readLine()) != null)
                strBuf.append(line + "");
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strBuf.toString();
    }



}
